package com.cheng.operation.impl;

import com.cheng.common.exception.CommonException;
import com.cheng.common.utils.StringUtil;
import com.cheng.data.entity.PermInfoEntity;
import com.cheng.data.vo.MenuVO;
import com.cheng.data.vo.PermInfoVO;
import com.cheng.operation.MenuOperation;
import com.cheng.service.PermInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author: ChengJW
 * @date: 2021/11/4 7:40
 * @version: 1.0
 * @description: 菜单相关的请求处理
 */
@Service
public class MenuOperationImpl implements MenuOperation {

    private final static Logger log = LoggerFactory.getLogger(MenuOperationImpl.class);

    @Autowired
    private PermInfoService permService;


    /**
     * @author: ChengJW
     * @params: []
     * @return: java.util.List<com.cheng.data.vo.MenuVO>
     * @date: 2021/9/1
     * @description: 查询所有的菜单信息，并分叉树的数据格式返回
     */
    @Override
    public List<MenuVO> selectMenuInfo(String urNm) throws CommonException {

        //将菜单信息存入集合并返回
        List<MenuVO> list = new ArrayList<>();
        //查询1级菜单信息
        List<PermInfoVO> permInfoEntityListLevel1 = null;
        permInfoEntityListLevel1 = permService.selectPermInfoByPrNm(urNm);
        if (permInfoEntityListLevel1.size()>0){
            //遍历1级菜单
            for (PermInfoVO permInfoVO : permInfoEntityListLevel1){
                String prOpsLevelNm = permInfoVO.getPrUpsLevelNm();
                //查询2级菜单信息(包含2级菜单对应的1级菜单)
                List<PermInfoVO> permInfoEntityListLevel2 = null;
                permInfoEntityListLevel2 = permService.selectPermInfoByPrUpsLevelNm(prOpsLevelNm,urNm);
                //遍历2级菜单，将权限信息存入 menuVo
                List<MenuVO> menuVOList2 = new ArrayList<>();
                Iterator<PermInfoVO> iterator = permInfoEntityListLevel2.iterator();
                while (iterator.hasNext()){
                    PermInfoVO permInfoEntity = iterator.next();
                    //去掉1级菜单信息
                    if (StringUtil.isNull(permInfoEntity.getPrLevelNm())){
                        iterator.remove();
                        continue;
                    }
                    MenuVO level2VO = new MenuVO();
                    level2VO.setMenuId(permInfoEntity.getPrId());
                    level2VO.setMenuNm(permInfoEntity.getPrNm());
                    level2VO.setMenuUrl(permInfoEntity.getPrUrl());
                    level2VO.setMenuDesc(permInfoEntity.getPrDesc());
                    //没有3级菜单
                    level2VO.setMenuVOList(null);
                    //添加2级菜单信息
                    menuVOList2.add(level2VO);
                }
                //创建1级菜单信息
                MenuVO level1VO = new MenuVO();
                //菜单编号
                level1VO.setMenuId(permInfoVO.getPrId());
                //菜单名称
                level1VO.setMenuNm(permInfoVO.getPrNm());
                //菜单url
                level1VO.setMenuUrl(permInfoVO.getPrUrl());
                //菜单中文描述
                level1VO.setMenuDesc(permInfoVO.getPrDesc());
                //下级菜单集合
                level1VO.setMenuVOList(menuVOList2);
                list.add(level1VO);
            }
        }
        return list;
    }
}
